<template>
  <el-dialog :visible.sync="dialog" :modal="false" :close-on-click-modal="false" :title="'控件权限设置(' + form.fieldVO.fieldName + ')'" width="50%" top="10px" append-to-body>
    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <div id="opt" style="float: left">
          <el-radio-group v-model="opt" size="mini">
            <el-radio-button label="visibleRadio">可见性</el-radio-button>
            <el-radio-button label="editRadio">可编辑</el-radio-button>
          </el-radio-group>
        </div>
      </div>
      <el-form v-show="opt === 'visibleRadio'" ref="visibleform" :model="form.visibleFieldPrivVO" size="small" label-width="160px">
        <el-form-item label="权限类型" prop="typeCode">
          <el-select v-model="form.visibleFieldPrivVO.typeCode" placeholder="请选择类型">
            <el-option
              v-for="(item) in dicts.form_priv_type_code"
              :key="item.id"
              :label="item.dictName"
              :value="item.dictCode"/>
          </el-select>
        </el-form-item>
        <el-form-item label="权限用户" prop="typeValue">
          <el-input v-if="form.visibleFieldPrivVO.typeCode === 'USER'" :readonly="true" v-model="form.visibleFieldPrivVO.typeName" placeholder="请选择用户">
            <el-button slot="append" icon="el-icon-search" @click="selectUser" />
          </el-input>
          <el-input v-else-if="form.visibleFieldPrivVO.typeCode === 'ROLE'" :readonly="true" v-model="form.visibleFieldPrivVO.typeName" placeholder="请选择角色">
            <el-button slot="append" icon="el-icon-search" @click="selectRole" />
          </el-input>
        </el-form-item>
      </el-form>
      <el-form v-show="opt === 'editRadio'" ref="editform" :model="form.editFieldPrivVO" size="small" label-width="160px">
        <el-form-item label="权限类型" prop="typeCode">
          <el-select v-model="form.editFieldPrivVO.typeCode" placeholder="请选择类型">
            <el-option
              v-for="(item) in dicts.form_priv_type_code"
              :key="item.id"
              :label="item.dictName"
              :value="item.dictCode"/>
          </el-select>
        </el-form-item>
        <el-form-item label="权限用户" prop="typeValue">
          <el-input v-if="form.editFieldPrivVO.typeCode === 'USER'" :readonly="true" v-model="form.editFieldPrivVO.typeName" placeholder="请选择用户">
            <el-button slot="append" icon="el-icon-search" @click="selectUser" />
          </el-input>
          <el-input v-else-if="form.editFieldPrivVO.typeCode === 'ROLE'" :readonly="true" v-model="form.editFieldPrivVO.typeName" placeholder="请选择角色">
            <el-button slot="append" icon="el-icon-search" @click="selectRole" />
          </el-input>
        </el-form-item>
      </el-form>
    </el-card>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSave">保存</el-button>
    </div>
    <userSelectList ref="userSelectList" :mult_select="true" @userSelectCallback="userSelectCallback" />
    <roleSelectList ref="roleSelectList" :mult_select="true" @roleSelectCallback="roleSelectCallback" />
  </el-dialog>
</template>

<script>
import { doSaveFieldPriv, getFieldPrivInfo } from '@/api/system/formSet'
import userSelectList from '@/views/system/user/module/userSelectList'
import roleSelectList from '@/views/system/role/module/roleSelectList'

export default {
  components: { userSelectList, roleSelectList },
  dictTypes: ['form_priv_type_code'],
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      formLoading: false,
      dialog: false,
      opt: 'visibleRadio',
      form: {
        fieldVO: {
          fieldName: ''
        },
        visibleFieldPrivVO: {
          privType: 'VISIBLE',
          typeCode: 'USER',
          typeName: '',
          typeValue: ''
        },
        editFieldPrivVO: {
          privType: 'EDIT',
          typeCode: 'USER',
          typeName: '',
          typeValue: ''
        }
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore(fieldVO) {
      this.form.fieldVO = fieldVO
      getFieldPrivInfo(fieldVO.id).then(res => {
        this.dialog = true
        this.form = res.data
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    selectUser() {
      const _this = this.$refs.userSelectList
      _this.init()
      _this.dialog = true
    },
    userSelectCallback(selectedRows, userSelectObj) {
      let approveName = ''
      let approveValue = ''
      selectedRows.forEach(row => {
        approveValue = row.userAccount + ',' + approveValue
        approveName = row.userName + ',' + approveName
      })
      if (this.opt === 'visibleRadio') {
        this.form.visibleFieldPrivVO.typeName = approveName
        this.form.visibleFieldPrivVO.typeValue = approveValue
      } else {
        this.form.editFieldPrivVO.typeName = approveName
        this.form.editFieldPrivVO.typeValue = approveValue
      }
      userSelectObj.cancel()
      userSelectObj.loading = false
    },
    selectRole() {
      const _this = this.$refs.roleSelectList
      _this.init()
      _this.dialog = true
    },
    roleSelectCallback(selectedRows, roleSelectObj) {
      let approveName = ''
      let approveValue = ''
      selectedRows.forEach(row => {
        approveValue = row.id + ',' + approveValue
        approveName = row.roleName + ',' + approveName
      })
      if (this.opt === 'visibleRadio') {
        this.form.visibleFieldPrivVO.typeName = approveName
        this.form.visibleFieldPrivVO.typeValue = approveValue
      } else {
        this.form.editFieldPrivVO.typeName = approveName
        this.form.editFieldPrivVO.typeValue = approveValue
      }
      roleSelectObj.cancel()
      roleSelectObj.loading = false
    },
    doSave() {
      doSaveFieldPriv(this.form).then(res => {
        this.resetForm()
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['visibleform'].resetFields()
      this.$refs['editform'].resetFields()
    }
  }
}
</script>
